package com.example.sonone.util;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

/* ━━━━━━佛祖保佑━━━━━━
 *                  ,;,,;
 *                ,;;'(    社
 *      __      ,;;' ' \   会
 *   /'  '\'~~'~' \ /'\.)  主
 * ,;(      )    /  |.     义
 *,;' \    /-.,,(   ) \    码
 *     ) /       ) / )|    农
 *     ||        ||  \)
 *     (_\       (_\
 * ━━━━━━永无BUG━━━━━━
 * @author ：zjq
 * @date ：2020/6/10 11:17
 * @description: TODO
 * @version: V1.0
 * @slogan: 天下风云出我辈，一入代码岁月催
 */
public class OssUtil {

    private static Logger LOGGER = LoggerFactory.getLogger(OssUtil.class);

    // 公司的oss
    private static String endpoint = "oss-cn-hangzhou.aliyuncs.com";        //测试环境
    private static String accessKeyId = "LTAI4FdUSfFxCFqEkVEqLHu4";
    private static String accessKeySecret = "mMzfT7CxP6XOYxc9IW0CHng1pDLJ7F";
    private static String ossBucket = "appointsyspic";
    private static String objectName = "tailTruck/11010110011/999991212/tailTruckTransportationtripPicPath.png";


    /**
     * 流式  下载
     *
     * @param objectName
     * @param res
     */
    public static String getPhotoStream(String objectName, HttpServletResponse res) throws IOException {
        StringBuilder objectContent = new StringBuilder();
        if (null != objectName) {
            OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
            // ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
            OSSObject ossObject = ossClient.getObject(ossBucket, objectName);
            BufferedReader reader = new BufferedReader(new InputStreamReader(ossObject.getObjectContent()));

            while (true) {
                String line = reader.readLine();
                if (line == null) break;
                objectContent.append(line);
            }
            reader.close();
            ossClient.shutdown();
        }
        return objectContent.toString();
    }

    public static void getStream(String objectName, HttpServletResponse res) {
        InputStream inputStream = null;
        if (StringUtils.isNotBlank(objectName)) {
            OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
            try {
                OutputStream outputStream = res.getOutputStream();
                OSSObject object = ossClient.getObject(ossBucket, objectName);
                inputStream = object.getObjectContent();

                int mark = -1;
                byte[] bytes = new byte[1024];
                while ((mark = inputStream.read(bytes)) != -1) {
                    outputStream.write(bytes, 0, mark);
                }
            } catch (Exception e) {
                LOGGER.error("getPicStream error",e);
//                try {
//                    res.getWriter().print(e);
//                } catch (IOException ex) {
//                    ex.printStackTrace();
//                }
            } finally {
                ossClient.shutdown();
            }
        }
    }


}
